﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.IO;
using System.Drawing;

namespace TARADesktopDoctor
{
    public partial class PatientSelectionPage
    {
        tara_wcf.ItaraServiceClient svc = new tara_wcf.ItaraServiceClient();

        //create patient container object here
        public static List<tara_wcf.patient> patientList = new List<tara_wcf.patient>();    //Full patient list of doctor
        public static List<tara_wcf.patient> searchedPatientList = new List<tara_wcf.patient>();    //Search result  of Patient List of doctor

        public PatientSelectionPage(tara_wcf.doctor doc)
        {
            this.InitializeComponent();
            grid_loading.Visibility = Visibility.Visible;
            lblUsername.Text = doc.FirstName + " " + doc.LastName;
            svc.retrieveAllOwnPatientsCompleted += new EventHandler<tara_wcf.retrieveAllOwnPatientsCompletedEventArgs>(svc_retrieveAllOwnPatientsCompleted);
            svc.retrieveAllOwnPatientsAsync(doc.Uid);
        }

        public PatientSelectionPage()
        {
            this.InitializeComponent();
        }

        public static bool editted; //Check if patient details has been editted
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void btnNewPatient_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new CreatePatient(LoginPage.doc.Uid));
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //Update panel if patient details is editted in EditPatient.xaml or new patient added in CreatePatient.xaml
            if (editted == true)
            {
                patientPanel.Children.Clear();
                foreach (tara_wcf.patient p in patientList)
                {
                    PatientItem pi = new PatientItem(p);
                    patientPanel.Children.Add(pi);
                }
                editted = false;
            }
        }

        void svc_retrieveAllOwnPatientsCompleted(object sender, tara_wcf.retrieveAllOwnPatientsCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                patientList = e.Result;
                foreach (tara_wcf.patient p in patientList)
                {
                    PatientItem pi = new PatientItem(p);
                    patientPanel.Children.Add(pi);
                }
                grid_loading.Visibility = Visibility.Collapsed;
            }
        }

        private void btnLogout_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //Clear all static variables
            NavigationService.Navigate(new LoginPage());
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (tbxSearch.Text.Trim().Length != 0)
            {
                svc.searchPatientCompleted += new EventHandler<tara_wcf.searchPatientCompletedEventArgs>(svc_searchPatientCompleted);
                svc.searchPatientAsync(tbxSearch.Text, LoginPage.doc.Uid);
            }
        }

        void svc_searchPatientCompleted(object sender, tara_wcf.searchPatientCompletedEventArgs e)
        {
            if (e.Error == null && e.Result != null)
            {
                //Reset panel and show search result
                searchedPatientList = e.Result;
                patientPanel.Children.Clear();
                foreach (tara_wcf.patient p in searchedPatientList)
                {
                    PatientItem pi = new PatientItem(p);
                    patientPanel.Children.Add(pi);
                }
            }
        }

        private void tbxSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (tbxSearch.Text.Trim().Length != 0)
            {
                if (e.Key == Key.Enter)
                {
                    svc.searchPatientCompleted += new EventHandler<tara_wcf.searchPatientCompletedEventArgs>(svc_searchPatientCompleted);
                    svc.searchPatientAsync(tbxSearch.Text, LoginPage.doc.Uid);
                }
            }
        }

        private void btnAll_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            patientPanel.Children.Clear();
            //Reset search filter
            foreach (tara_wcf.patient p in patientList)
            {
                PatientItem pi = new PatientItem(p);
                patientPanel.Children.Add(pi);
            }
        }

        private void tbxSearch_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            if (tbxSearch.Text.Trim().ToString() == "")
            {
                tbxSearch.Text = "Enter a search item";
            }
        }

        private void tbxSearch_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            if (tbxSearch.Text.Trim().ToString() == "Enter a search item")
            {
                tbxSearch.Text = "";
            }
        }
    }
}